﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyCommerce.ManagementBase.Common.Base.Common;
using MyCommerce.Presentation.GeneralParameters.IViews;
using MyCommerce.Presentation.GeneralParameters.Presenters;
using Innova.Microfinanzas.BusinessEntities;

namespace MyCommerce.Windows.GeneralParameters.Views
{
    public partial class CurrencyExchangeView : BaseForm, ICurrencyExchangeView
    {
        private CurrencyExchangePresenter pPresenter;
        private bool isUpdate = false;

        public CurrencyExchangeView()
        {
            InitializeComponent();
            pPresenter = new CurrencyExchangePresenter(this);
        }

        #region ICurrencyExchangeView Members

        private CurrencyExchangeRates vobjClassifier;
        public CurrencyExchangeRates objExchangeRate
        {
            get
            {
                vobjClassifier = new CurrencyExchangeRates();
                if (isUpdate)
                    vobjClassifier.IdCurrencyExchangeRates = (long)grvData.ActiveRow.Cells["IdCurrencyExchangeRates"].Value;
                vobjClassifier.BuyExchangeRate = Convert.ToDecimal(fieldExchangeRateBuy.Value);
                vobjClassifier.SellExchangeRate = Convert.ToDecimal(fieldExchangeRateSell.Value);
                vobjClassifier.IdcMoney = (long)listMoney.SelectedValue;
                return vobjClassifier;
            }
            set
            {
                fieldExchangeRateBuy.Value = value.BuyExchangeRate;
                fieldExchangeRateSell.Value = value.SellExchangeRate;
                if (String.IsNullOrEmpty(listMoney.ValueMember) == false)
                    listMoney.SelectedValue = value.IdcMoney;
            }
        }

        public List<CurrencyExchangeRates> ColExchangeRates
        {
            set
            {
                grvData.DataSource = value;
            }
        }

        public List<Classifier> colMoneys
        {
            set
            {                
                listMoney.DisplayMember = "Name";
                listMoney.ValueMember = "IdClassifier";
                listMoney.DataSource = value;
            }
        }

        public new event EventHandler OnLoad;

        public event OnEntityEventHandler OnEdit;

        public event OnEntityEventHandler OnDelete;

        public event OnSaveEventHandler OnSave;

        #endregion

        /// <summary>
        /// Validacion de los datos.
        /// </summary>
        /// <returns></returns>
        private Boolean DataAutoValidate()
        {
            bool result = true;
            StringBuilder message = new StringBuilder();

            if (Convert.ToDecimal(fieldExchangeRateBuy.Value) <= 0)
            {
                result = false;
                message.AppendLine("Debe ingresar un tipo de cambio para la compra.");
            }

            if (Convert.ToDecimal(fieldExchangeRateSell.Value) <= 0)
            {
                result = false;
                message.AppendLine("Debe ingresar un tipo de cambio para la venta.");
            }

            if (listMoney.SelectedIndex == -1)
            {
                result = false;
                message.AppendLine("Debe elegir una moneda.");
            }

            if (!result)
            {
                Context.ShowErrorMessageBox(message.ToString(), "Validación de datos.");
            }

            return result;
        }

        /// <summary>
        /// Modifica el estado de edicion de la pantalla.
        /// </summary>
        /// <param name="edit"></param>
        private void Modify(bool edit)
        {
            grvData.Enabled = !edit;
            fieldExchangeRateBuy.Enabled = edit;
            fieldExchangeRateSell.Enabled = edit;
            listMoney.Enabled = edit;
        }

        /// <summary>
        /// Limpia los campos.
        /// </summary>
        private void CleanScreen()
        {
            fieldExchangeRateBuy.Value = null;
            fieldExchangeRateSell.Value = null;
            listMoney.SelectedIndex = -1;
        }

        private void CurrencyExchangeView_Load(object sender, EventArgs e)
        {
            if (OnLoad != null)
            {
                OnLoad(sender, e);
                Modify(false);
            }
        }

        private void tsCancelButtonToolStrip_Click(object sender, EventArgs e)
        {
            isUpdate = false;
            commonButtonsToolStrip1.ToggleButtons();
            Modify(false);
            CleanScreen();
        }

        private void tsCloseButtonToolStrip_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void tsDeleteButtonToolStrip_Click(object sender, EventArgs e)
        {
            try
            {
                if (grvData.ActiveRow.Band.Index == 0)
                {
                    Context.ShowInformationMessageBox("Debe seleccionar un nodo hijo.", "Validación de datos.");
                }
                else
                {
                    if (grvData.ActiveRow.Selected)
                    {
                        if (Context.ShowQuestionMessageBox("¿Desea eliminar a este tipo de cambio?", "Eliminar tipo de cambio") == System.Windows.Forms.DialogResult.Yes)
                        {
                            OnDelete(sender, (long)grvData.ActiveRow.Cells["IdCurrencyExchangeRates"].Value);
                            Context.ShowInformationMessageBox("El tipo de cambio se eliminó correctamente.", "Eliminar tipos de cambio");
                        }
                    }
                    else
                    {
                        Context.ShowInformationMessageBox("Debe seleccionar un tipo de cambio.", "Validación de datos.");
                    }
                }
            }
            catch (Exception ex)
            {
                Context.HandleException(ex);
            }            
        }

        private void tsNewButtonToolStrip_Click(object sender, EventArgs e)
        {
            commonButtonsToolStrip1.ToggleButtons();
            Modify(true);
        }

        private void tsSaveButtonToolStrip_Click(object sender, EventArgs e)
        {
            try
            {
                if (!DataAutoValidate())
                    return;

                if (!isUpdate)
                {
                    if (OnSave != null)
                    {
                        OnSave(this, true);
                        Context.ShowInformationMessageBox("La información se guardó correctamente.", "Creación/Edición de parametros.");
                    }

                }
                else
                {
                    if (OnSave != null)
                    {
                        OnSave(this, false);
                        Context.ShowInformationMessageBox("La información se actualizó correctamente.", "Creación/Edición de parametros.");
                    }
                }
                isUpdate = false;
                commonButtonsToolStrip1.ToggleButtons();
                Modify(false);
                CleanScreen();
            }
            catch (Exception ex)
            {
                Context.HandleException(ex);
            }
        }
    }
}
